package idv.arthur.JDBC;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class KeyGet {

	public static void main(String[] args) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			System.out.print("Connecting...");
			System.out.println("OK");
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(
					"jdbc:mysql://10.144.136.54:3306/dbtest", "dbadmin",
					"mysql");
			
			String sql = "INSERT INTO nt (nam) VALUES('aaa')";
			ps = conn.prepareStatement(sql,new String[] { "priCol" });
			
			ps.executeUpdate();
			rs = ps.getGeneratedKeys();
			rs.next();
			System.out.println(rs.getLong(1));
			
			System.out.println("Disconned");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if ( rs != null ) { rs.close(); }	
				if ( ps != null ) { ps.close(); }
				if ( conn != null ) { conn.close(); }
			} catch(Exception e) {
				e.printStackTrace();
			}
			
		}

	}

}
